package com.xirui.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.xirui.bean.Paymaster;
import com.xirui.bean.Roll;
import com.xirui.bean.Structure;
import com.xirui.mapper.RollMapper;
import com.xirui.service.RollService;

@Controller
public class RollController {

	@Autowired
	private RollService rollService;
	@Autowired
	private RollMapper rollMapper;
	
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(defaultValue="1")Integer pageNum,Roll roll){
		PageInfo<Roll> list=rollService.selectRoll(pageNum,roll);
		model.addAttribute("pg", list);
		model.addAttribute("roll", roll);
		return "list";
	}
	
	@RequestMapping("deleteRoll")
	@ResponseBody
	public int deleteRoll(Integer id){
		
		return rollService.deleteRoll(id);
	}
	@RequestMapping("sanji")
	@ResponseBody
	public List<Structure> sanji(Integer id){
		
		return rollService.sanji(id);
	}
	
	
	@RequestMapping("toAdd")
	public String toAdd(Model model){
		model.addAttribute("roll", new Roll());
		return "add";
	}
	@RequestMapping("add")
	public String addRoll(@ModelAttribute("roll")Roll roll,Model model,MultipartFile file) throws IllegalStateException, IOException{
		rollService.addRoll(roll,file);
		return "redirect:list";
	}
	
	@RequestMapping("toUp")
	public String toUp(Integer id,Model model){
		Roll roll=rollMapper.selRoll1(id);
		model.addAttribute("roll", roll);
		List<Paymaster> pay=rollService.selRoll(id);
		model.addAttribute("pay", pay);
		
		return "update";
	}
	@RequestMapping("update")
	public String update(@ModelAttribute("roll")Roll roll,MultipartFile file){
		System.err.println(roll);
		rollService.updateRoll(roll,file);
		return "redirect:list";
	}
	
	
	
	
}
